<template>
  <view>
    <uni-popup ref="signupPop" type="center">
      <view class="w696 circle54 bg-f8ff ov-hd show0">
        <view class="w-100 h146 f-c-c radius-54-botton font40 t-0 font600">
          注册购票信息
        </view>
        <view class="w-100 h426 re">
          <view class="w-100 h80 f-s-c mb-2" style="padding: 0 30rpx">
            <!-- <view class="w100 font28">手机号：</view> -->
            <input
              style="width: 100%"
              class="uni-input border h80 font28 px-2"
              v-model="phone"
              placeholder="请输入手机号码"
            />
            <!-- <button
              class="px-05 w226 bg-theme h80 font24 f-c-c"
              size="mini"
              open-type="getPhoneNumber"
              @getphonenumber.stop="getPhoneNumber"
            >
              获取手机号
            </button> -->
          </view>
          <view class="w-100 f-c-c mt-14 mb-2 font28" style="margin-top: 40px">
            <view class="f-c-c">
              <checkbox
                style="transform: scale(0.7)"
                @click="is_ok = !is_ok"
                :checked="is_ok"
              ></checkbox
              >我已同意
              <text @click="get_sers('yonghu')" class="text-red text-line"
                >《用户协议》</text
              >和<text class="text-red text-line" @click="get_sers('yingsi')"
                >《隐私声明》</text
              >
            </view>
          </view>
          <view class="w-100 h80 f-c-c pt-4">
            <view
              class="w270 h90 bg-theme font32 t-20 font600 f-c-c circle14"
              @click="get_register_info()"
              >同意注册</view
            >
          </view>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
import { getMiniPhoneByCode, member_add, loginInfo } from "@/api/auth.js";
import { mapState, mapActions } from "vuex";
export default {
  props: {
    isRegister: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      phone: "",
      is_ok: false,
    };
  },
  methods: {
    open() {
      this.$refs.signupPop.open();
    },
    close() {
      this.$refs.signupPop.close();
    },
    ...mapActions("user", ["set_change_is_login"]),
    getPhoneNumber(e) {
      if (e.detail.errMsg == "getPhoneNumber:ok") {
        getMiniPhoneByCode({ code: e.detail.code }).then((res) => {
          if (res.code == 0) {
            this.phone = res.body.phone_info.phoneNumber;
          } else {
            this.phone = "";
          }
        });
      } else {
        this.phone = "";
      }
    },
    get_sers(val) {
      uni.navigateTo({
        url: "/pages/my/set/" + val,
      });
    },
    get_sers(val) {
      uni.navigateTo({
        url: "/pages/my/set/" + val,
      });
    },
    get_register_info() {
      if (!this.is_ok) {
        return uni.showToast({
          title: "请先同意用户协议和隐私声明",
          icon: "none",
        });
      }
      let userInfo = uni.getStorageSync("userInfo");
      let inviter = uni.getStorageSync("inviter");
      //验证手机号格式
      const reg = /^1[3-9]\d{9}$/;
      if (!reg.test(this.phone)) {
        return uni.showToast({
          title: "请输入正确的手机号",
          icon: "none",
        });
      }
      let data = {
        phone: this.phone,
        inviteId: inviter || "",
      };
      member_add(data).then((res) => {
        if (res.code == 0) {
          uni.setStorageSync("userInfo", res.body);
          uni.showToast({
            title: "注册成功",
            icon: "none",
          });
          this.get_login_info_loading();
          this.close();
        } else {
          uni.showToast({
            title: res.message,
            icon: "none",
          });
        }
      });
    },
    get_login_info_loading() {
      loginInfo({})
        .then((res) => {
          if (res.code == 0) {
            uni.setStorageSync("token", res.body.token); //存token
            // 是否 已注册
            uni.setStorageSync("is_user", !!res.body.phone);
            let userInfo = {
              ...res.body,
              token: res.body?.token,
              openId: res.body?.openId
            };
            // 先存用户信息
            uni.setStorageSync("userInfo", userInfo);
            this.userInfo = userInfo;
            this.$emit("update:isRegister", true);
            // 在设置是否登录 是否登录需要判断是否有用户信息
            this.set_change_is_login && this.set_change_is_login(true);
          }
        })
        .catch((e) => {
          console.error(e);
        })
        .finally(() => {
          uni.hideLoading();
        });
    },
  },
};
</script>

<style lang="scss" scoped></style>
